package com.psp.park.controller;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.northend.util.NumUtil;
import com.psp.park.controller.res.BaseResult;
import com.psp.park.controller.res.ListResult;
import com.psp.park.controller.res.bean.RVisiterBean;
import com.psp.park.controller.springmvc.req.GetUserListParam;
import com.psp.park.controller.springmvc.req.RegisterParam;
import com.psp.park.controller.springmvc.req.SendPhoneCodeParam;
import com.psp.park.service.VisitorService;
import com.psp.park.service.exception.ServiceException;
import com.psp.park.service.impl.PageResult;

@Component
public class VisitorController {
	Logger logger = Logger.getLogger(VisitorController.class);

	@Autowired
	VisitorService visitorServiceImpl;

	public BaseResult register(RegisterParam param, HttpServletRequest request, HttpServletResponse response) {
		BaseResult result = new BaseResult();
		String token = (String) request.getAttribute("token");
		boolean flag = false;
		try {
			String userName = param.getUserName();
			String account = param.getAccount();
			String phoneCode = param.getPhoneCode();
			String imgKey = param.getImgKey();
			String imgCode = param.getImgCode();
			String userId = (String) request.getAttribute("userId");
			visitorServiceImpl.checkImgCode(imgKey, imgCode);
			flag = visitorServiceImpl.reg(userId, userName, account, phoneCode, imgKey, imgCode);
			result.setFlag(flag);
		} catch (ServiceException e) {
			result.setFlag(false);
			result.setMsg(e.getServiceMsg());
		}
		result.setToken(token);
		return result;
	}

	public ListResult<RVisiterBean> list(GetUserListParam param, HttpServletRequest request,
			HttpServletResponse response) {
		ListResult<RVisiterBean> result = new ListResult<>();
		String token = (String) request.getAttribute("token");
		try {
			String uid = (String) request.getAttribute("userId");
			int page = NumUtil.toInt(param.getPage(), 0);
			int pageSize = NumUtil.toInt(param.getPageSize(), 10);
			PageResult<RVisiterBean> resList = visitorServiceImpl.getList(page, pageSize, uid);

			result.setData(resList.getData());
			result.setTotalSize(resList.getCount());
			result.setPage(page);
		} catch (ServiceException e) {
			result.setServiceException(e);
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			result.setServiceException(new ServiceException("error", "发生异常"));
		}
		result.setToken(token);
		return result;
	}

	public BaseResult sendPhoneCode(SendPhoneCodeParam param, HttpServletRequest request,
			HttpServletResponse response) {
		BaseResult result = new BaseResult();
		try {
			String phone = param.getPhone();
			String imgKey = param.getImgKey();
			String imgCode = param.getImgCode();
			visitorServiceImpl.checkImgCode(imgKey, imgCode);
			boolean flag = visitorServiceImpl.sendVCode(phone);
			result.setFlag(flag);
		} catch (ServiceException e) {
			result.setServiceException(e);
		}
		return result;
	}
}
